package com.example.hospitalmanagement.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.example.hospitalmanagement.R
import com.example.hospitalmanagement.bean.DeskDateItem

class DeskDoctorDateAdapter(context: Context) :
    RecyclerView.Adapter<DeskDoctorDateAdapter.DateHolder>() {
    private val context = context
    lateinit var dataList: MutableList<DeskDateItem>
    private lateinit var listener: (position: Int) -> Unit
    fun setListener(listener: (position: Int) -> Unit) {
        this.listener = listener
    }

    class DateHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val deskDataWeek: TextView = itemView.findViewById(R.id.desk_data_week)
        val deskDataDay: TextView = itemView.findViewById(R.id.desk_data_day)
        val deskDataBot: ImageView = itemView.findViewById(R.id.desk_data_bot)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DateHolder {
        val dateHolder = DateHolder(
            LayoutInflater.from(context).inflate(R.layout.desk_date_items, parent, false)
        )
        dateHolder.itemView.setOnClickListener {
            listener(dateHolder.adapterPosition)
        }

        return dateHolder
    }

    override fun onBindViewHolder(holder: DateHolder, position: Int) {
        holder.deskDataWeek.text = dataList[position].dateWeek
        holder.deskDataDay.text = dataList[position].dataDay
        if (dataList[position].botGoneAndVisible) {
            holder.deskDataBot.isVisible = true
        } else {
            holder.deskDataBot.isGone = true
        }
        if (dataList[position].checked){
            holder.itemView.setBackgroundResource(R.drawable.schedule_doctor_date_background)
            holder.deskDataWeek.setTextColor(context.getColor(R.color.white))
            holder.deskDataDay.setTextColor(context.getColor(R.color.white))
        }else{
            holder.itemView.setBackgroundColor(context.getColor(R.color.white))
            holder.deskDataWeek.setTextColor(context.getColor(R.color.black))
            holder.deskDataDay.setTextColor(context.getColor(R.color.black))
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}